package sunwin.yoms.web.controller.publicty;

import jodd.datetime.JDateTime;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import sunwin.yoms.comm.YomsException;
import sunwin.yoms.comm.YomsFunction;
import sunwin.yoms.comm.YomsPage;
import sunwin.yoms.domain.publicty.VideoDomain;
import sunwin.yoms.service.publicty.VideoService;
import sunwin.yoms.web.comm.R;
import sunwin.yoms.web.comm.YomsAjaxAnswer;
import sunwin.yoms.web.comm.YomsController;
import sunwin.yoms.web.comm.YomsControllerDefaultData;
import sunwin.yoms.web.comm.resolverArg.YomsDateParam;
import sunwin.yoms.web.controller.publicty.xml.VcastrRoot;
import sunwin.yoms.web.model.VideoListModel;

import java.util.Date;
import java.util.List;

/**
 * Created by watson on 2015/7/29.
 */
@Controller
public class VideoController {
    @Autowired
    private VideoService videoService;
    @Autowired
    private YomsControllerDefaultData defaultData;

    private final static String VideoViewName = "publicty/video";
    private final static String UploadVideoViewName = "publicty/video.upload";

    /**
     * 视频管理界面
     *
     * @return
     */
    @RequestMapping(value = R.Video, method = RequestMethod.GET)
    @YomsController(functionNo = YomsFunction.FindVideo, viewName = VideoViewName)
    public ModelAndView video(VideoDomain videoDomain,
                              @RequestParam(required = false, defaultValue = "1") int pageIndex,
                              @YomsDateParam JDateTime startTime,
                              @YomsDateParam JDateTime endTime,
                              ModelMap modelMap) throws YomsException {
        modelMap.addAttribute("videos", videoDomain);
        modelMap.addAttribute("startTime", startTime == null ? null : startTime.toString("YYYY-MM-DD hh:mm"));
        modelMap.addAttribute("endTime", endTime == null ? null : endTime.toString("YYYY-MM-DD hh:mm"));
        YomsPage yomsPage = new YomsPage();
        yomsPage.setPageIndex(pageIndex);
        yomsPage.setPageSize(8);
        Date date1 = startTime == null ? null : startTime.convertToDate();
        Date date2 = endTime == null ? null : endTime.convertToDate();
        List<VideoDomain> videoDomains = videoService.getVideoDomain(videoDomain, yomsPage, date1, date2);
        ModelAndView mav = new ModelAndView();
        mav.addObject("yomsPage", yomsPage);
        mav.addObject("videoList", videoDomains);
        return mav;
    }

    /**
     * Ajax异步请求
     */
    @RequestMapping(value = "video", name = "publicty/news.create", method = RequestMethod.GET, produces = {"text/html;charset=UTF-8"})
    @ResponseBody
    public String ajaxTest(@RequestParam(required = false, defaultValue = "1") int pageIndex) throws YomsException {
        YomsPage yomsPage = new YomsPage();
        yomsPage.setPageIndex(pageIndex);
        yomsPage.setPageSize(3);

        List<VideoDomain> videoDomains = videoService.getVideoDomain(null, yomsPage, null, null);

        VideoListModel videoListModel = new VideoListModel();
        videoListModel.setVideos(videoDomains);
        videoListModel.setYomsPage(yomsPage);

        JSONObject jsonObj = JSONObject.fromObject(videoListModel);

        return jsonObj.toString();
    }


    /**
     * 上传界面
     *
     * @throws YomsException
     */
    @RequestMapping(value = R.UploadVideo, method = RequestMethod.GET)
    @YomsController(functionNo = YomsFunction.UploadVideo, viewName = UploadVideoViewName)
    public void upload() throws YomsException {
    }

    /**
     * 新增视频
     *
     * @param domain
     * @return
     * @throws Exception
     */
    @RequestMapping(value = R.AddVideo, method = RequestMethod.POST)
    @YomsController(functionNo = YomsFunction.AddVideo)
    @ResponseBody
    public YomsAjaxAnswer addVideo(VideoDomain domain) throws Exception {
        domain.setVideoUserId(defaultData.getCurrentYomsUserDetails().getUserid());
        videoService.addVideoDomain(domain);

        return YomsAjaxAnswer.getInstance();
    }

    /**
     * 删除视频
     *
     * @param domain
     * @return
     * @throws Exception
     */
    @RequestMapping(value = R.DelVideo, method = RequestMethod.POST)
    @YomsController(functionNo = YomsFunction.DelVideo)
    @ResponseBody
    public YomsAjaxAnswer delVideo(VideoDomain domain) throws Exception {
        videoService.deleteVideoDomain(domain.getVideoId());

        return YomsAjaxAnswer.getInstance();
    }

    /**
     * 上传视频
     *
     * @param file 视频文件
     * @return
     * @throws Exception
     */
    @RequestMapping(value = R.UploadVideo, method = RequestMethod.POST)
    @YomsController(functionNo = YomsFunction.UploadVideo)
    @ResponseBody
    public YomsAjaxAnswer upload(@RequestParam("file") MultipartFile file) throws YomsException {
        YomsAjaxAnswer answer = new YomsAjaxAnswer();
        String fileName = videoService.SaveVideo(file);
        answer.setSuccess(true);
        answer.setData(fileName);
        return answer;
    }

    /**
     * 视频播放
     */
    @RequestMapping(value = "vcastr")
    public ModelAndView getVideoplayerParam(int id) {
        VcastrRoot vcastr = new VcastrRoot();
        ModelAndView mav = new ModelAndView();
        mav.setViewName("xml-view");
        mav.addObject("xmlObject", vcastr);
        try {
            String fileName = videoService.getVideoFileName(id);
            vcastr.getChannel().getItem().setSource("../video/" + fileName);
        } catch (YomsException e) {
            e.printStackTrace();
        }
        return mav;
    }

}

